import csv
import os
import time

class WriteFile(object):

    @staticmethod
    def resText(basis_inst):
        all_info = []
        for basis in basis_inst:
            insts = basis_inst[basis]
            error_so = insts[0].stack
            so_label = insts[0].process_name
            if insts[0].all_so_row_list is None:
                valid_fullso = None
            else:
                if insts[0].basis_so:
                    len_sonum = len(insts[0].basis_so)
                    valid_fullso = '\n'.join(insts[0].ori_solist[: len_sonum])
                else:
                    valid_fullso = '\n'.join(insts[0].ori_solist)
            count = len(insts)
            list_files = []
            for inst in insts:
                list_files.append(inst.path)
            for inst in insts:
                if inst.iscrucial:
                    if error_so:
                        error_so = f'关键进程\n崩溃so: {error_so}'
                        so_label = inst.process_name
                    break
            all_files = '\n'.join(list_files)
            if insts[0].foundstatus == 1:
                error_so = 'Exception'
            if insts[0].foundstatus == 3:
                error_so = 'Invalid So'
            if insts[0].foundstatus == 5:
                error_so = 'DFX related'
            res_basis = [error_so, so_label, valid_fullso, count, all_files]
            all_info.append(res_basis)
        return all_info

    @staticmethod
    def write(cppdir, basis_inst):
        '''
        comment: write result to txt and csv
        :param cppdir:
        :param basis_inst:
        :return:
        '''
        all_info = WriteFile.resText(basis_inst)
        now = time.strftime('%Y%m%d%H%M%S')
        txt_path = os.path.join(cppdir, f"ResultOfCppcrash{now}.txt")
        csv_path = os.path.join(cppdir, f"ResultOfCppcrash{now}.csv")
        WriteFile.writetxt(txt_path, all_info)
        WriteFile.writecsv(csv_path, all_info)

    @staticmethod
    def writetxt(txt_path, all_info):
        description = [
            '崩溃so: ',
            '进程: ',
            'so详情: ',
            '出现次数: ',
            '文件路径: '
        ]
        with open(txt_path, 'w', encoding='utf-8-sig') as fw:
            for cont in all_info:
                if cont == None:
                    fw.write('')
                else:
                    for i in range(len(description)):
                        if cont[i] is not None:
                            so_pri_info = description[i] + str(cont[i])
                            fw.write(so_pri_info + '\n')
                fw.write('\n')
        fw.close()
        print('txt_path: ', txt_path)

    @staticmethod
    def writecsv(csv_path, all_info):
        with open(csv_path, "w", newline="", encoding='utf-8-sig') as fcsv:
            writer = csv.writer(fcsv)
            writer.writerow(['崩溃so', '进程', 'so详情', '出现次数', '责任人', "领域", '文件路径'])
            for cont in all_info:
                cont[-1] = cont[-1].split('\n')[0]
                writer.writerow(cont)
        fcsv.close()
        print('csv_path: ', csv_path)